package com.uqbar.asteroids.components;

public class Vector2D {

	private double x;
	private double y;
	private double modulo;

	public Vector2D(double x, double y) {
		this.x = x;
		this.y = y;
		this.modulo = -1;
	}

	public double getModule() {
		if(this.modulo < 0) {
			this.modulo = Math.sqrt(Math.pow(x, 2) + Math.pow(y, 2));
		}
		return modulo;
	}
	
	public Vector2D producto(double valor){
		return new Vector2D(x * valor , y * valor);
	}

	public Vector2D suma(Vector2D vector2d) {
		return new Vector2D(this.x + vector2d.getX(), this.y + vector2d.getY());
	}
	
	public double getX() {
		return x;
	}
	public double getY() {
		return y;
	}
	
	public Vector2D toPolar() {
		return new Vector2D(this.getModule(), Math.atan2(y, x));
	}
	
	public Vector2D toCartesians() {
		return new Vector2D(x * Math.cos(y), x * Math.sin(y));
	}

	public double distancia(Vector2D other) {
		return other.resta(this).getModule();
	}

	public Vector2D resta(Vector2D vector2d) {
		return new Vector2D(this.x - vector2d.x, this.y - vector2d.y);
	}
	
	public void resetVector (){
		this.y= 0;
		this.x = 0;
	}
}
